package com.yulongtian.interviewWorks.day20221112;

import java.util.HashMap;

/**
 * https://leetcode.cn/problems/words-frequency-lcci/?favorite=xb9lfcwi
 * 可
 *
 * @author yulongTian
 * @create 2022-11-12 15:48
 */
public class WordsFrequency {

    private HashMap<String, Integer> myBook;

    public WordsFrequency(String[] book) {
        myBook = new HashMap<>();
        for (String s : book) {
            //为空
            if (myBook.isEmpty()) {
                myBook.put(s, 1);
            } else {
                //包含
                if (myBook.containsKey(s)) {
                    Integer num = myBook.get(s);
                    myBook.put(s, num + 1);
                } else {//不包含
                    myBook.put(s, 1);
                }
            }
        }

    }

    public int get(String word) {
        return myBook.getOrDefault(word, 0);
    }

}
